package com.music.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.dao.AlbumMapper;
import com.music.dao.GenresMapper;
import com.music.dao.SingerMapper;
import com.music.dao.SongMapper;
import com.music.entity.*;
import com.music.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SongServiceImpl implements SongService {
    @Autowired
    private SingerMapper singerMapper;
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private GenresMapper genresMapper;
    @Autowired
    private SongMapper songMapper;
    @Override
    public List<Song> SelectOrderByTime(Song song) {
        return songMapper.SelectByCondition(song);
    }

    @Override
    public Song fingById(String SongId) {
        return songMapper.selectByPrimaryKey(SongId);
    }

    @Override
    public List<Song> findBycondition(Map<String, Object> map) {
        return songMapper.findBycondition(map);
    }

    @Override
    public PageInfo<Song> getSongByPage(int pageNum, int pageSize, Song song) {
        StringBuffer songName = new StringBuffer();
        // 判断参数情况，拼接字符串匹配符%
        if (song.getSongName() != null && !"".equals(song.getSongName())) {
            // 进行管理员账号关键字查询
            songName.append("%").append(song.getSongName()).append("%");// where A.SingerName like '%歌曲名称%';
            // 重新设值
            song.setSongName(songName.toString());
        }
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        //执行select查询语句
        List<Song> pageSelective = songMapper.findPageSelective(song);
        //封装分页对象
        PageInfo<Song> pageInfo = new PageInfo<>(pageSelective);
        return pageInfo;
    }

    @Override
    public boolean addSong(Song song) {
        return songMapper.byInsert(song)>0;
    }

    @Override
    public boolean modifySong(Song song) {
        return songMapper.byUpdate(song)>0;
    }

    @Override
    public Song getById(String songId) {
        Song song=songMapper.selectByPrimaryKey(songId);
        if (song!=null){

            if (song.getAlbumId()!=null){
                Album album= albumMapper.selectByPrimaryKey(song.getAlbumId());
                song.setAlbum(album);
            }
            if (song.getGenresId()!=null){
                Genres genres=genresMapper.queryById(song.getGenresId());
                song.setGenres(genres);
            }
            if (song.getSingerId()!=null){
                Singer singer=singerMapper.selectByPrimaryKey(song.getSingerId());
                song.setSinger(singer);
            }
        }
        return song;
    }
}
